package com.kefu.service.impl;

import com.kefu.common.CustomizeException;
import com.kefu.mapper.JurisdictionMapper;
import com.kefu.model.Jurisdiction;
import com.kefu.service.JurisdictionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by Administrator on 2016/12/2.
 */
@Service("jurisdictionService")
public class JurisdictionServiceImpl implements JurisdictionService {
    @Autowired
    private JurisdictionMapper jurisdictionMapper;


    @Override
    public List<Jurisdiction> selectAll() {
        return jurisdictionMapper.selectAll();
    }

    @Override
    public void updateById(Jurisdiction jurisdiction) {
        if(jurisdiction!=null){
            if(StringUtils.isEmpty(jurisdiction.getFullName())){
                throw new CustomizeException("fullname不能为空 ");
            }
            if(StringUtils.isEmpty(jurisdiction.getParentId())){
                throw new CustomizeException("parentId不能为空 ");
            }
            List<Jurisdiction> parent=selectByParentId(jurisdiction.getParentId());
            if(parent!=null){
                jurisdiction.setPriority(parent.size());
            }else {
                jurisdiction.setPriority(0);
            }
            jurisdictionMapper.updateById(jurisdiction);
        }
    }
    @Override
    public Jurisdiction findById(int id) {
        return jurisdictionMapper.findById(id);
    }

    public List<Jurisdiction> selectByParentId(String parentId) {
        return jurisdictionMapper.selectByParentId(parentId);
    }
}
